function make_fit_mnc(data, fit, basename, template, limits, mask)
%
%  function make_fit_mnc(study, fit, basename, limits, mask )
%
%

var = { 'kf' 1; 'f' 1;  'R1' 1; 'T2' 1; 'T2' 2};
name = { 'kf' 'f' 'r1f' 't2f' 't2r'};

N = sqrt(length(data.mask));

if(nargin < 5 | isempty(limits))
  limits = [0 0 0 0 0; 10 0.5 10 1 5e-5]';
end

if(nargin < 6 | isempty(mask))
  mask = ones(size(data.mask));
end

data.mask = data.mask(:);

for i = 1:size(var,1)
    
  a = getfield(fit,var{i,1});
  img = zeros(size(data.mask));
  %img(data.mask) = clamp_img(a(:,var{i,2}), limits(i,:));
  mask_count=1;
  for ii = 1:size(img)
      if(data.mask(ii)==0)
          img(ii)=0;
      else
          img(ii)=a(mask_count);
          mask_count=mask_count+1;
      end
  end
  img=reshape(img,128,96,32);
  if i==2
      img(img>0.5)=0;
      temp=img(:,:,16);
      temp(temp==0)=[];
      temp(temp==0.5)=[];
      figure()
      imhist(temp,500)
      mean(temp(:))
      std(temp(:))
  end
  figure()
  imagesc(img(:,:,16))
  title(sprintf('%s',var{i,1}))
  pause(3)
end
a = sqrt(getfield(fit,'e2'));
img = zeros(size(data.mask));
img(data.mask) = clamp_img(a(:,1), [0 1]);

%h = newimage([basename '_rms.mnc'], [0 1 N N], template);
%h = newimage([basename '_rms.mnc'], [0 1], template);
%putimages(h, img, 1);
